﻿//===========================[ 8 / 18 / 2011 LVI ]=========================
//				Copyright c Vitaly Lyaschenko <SkyChameleon@gmail.com>
//								Source file
// Purpose: 
//=========================================================================
#include "GameWorld.h"
#include "Kernel.h"
#include "cTimer.h"
#include "cSoundManager.h"

// Object
#include "cBaseObject.h"
#include "cDebugObject.h"

GameWorld::GameWorld(void) : 
	pSignShape(nullptr),
	pSignOldShape(nullptr),
	pAssemblyShape(nullptr),
	pCoorGrid(nullptr),
	pCamera(nullptr)
{
	m_pRender = Kernel::spec().getRender();
	m_pScript = Kernel::spec().getScript();

	cwLogger::writeINFO("Game World Create!");
}

GameWorld::~GameWorld(void)
{
	SAFE_DELETE(m_pEffect);
	SAFE_DELETE(pDebugEffect);
	SAFE_DELETE(pCoorGrid);

	SAFE_DELETE(pSignShape);
	//SAFE_DELETE(pSignOldShape);
	SAFE_DELETE(pAssemblyShape);
	SAFE_DELETE(pCamera);
	m_pRender = nullptr;
	m_pScript = nullptr;
}

bool GameWorld::InitWorld()
{
	m_pRender = Kernel::spec().getRender();
	const int w = cParserCfg::getRenderCnfg()->width;
	const int h = cParserCfg::getRenderCnfg()->height;
	m_pRender->setViewport( 0, 0, w, h );

	pCamera = new cCamera;
	pCamera->setPerspective( w / h );
	pCamera->setPosition(0, -10, -20);

	m_pEffect = new cEffect;
	m_pEffect->create( "baseEffect.lua" );

	pDebugEffect = new cEffect;
	pDebugEffect->create( "debugEffect.lua" );

	pSignShape = new Shape;
	if ( !pSignShape->init( "SignObject.lua", m_pEffect, true )) // true - создание VAO/VBO
		cwLogger::writeERROR("error init obj_Sign");
	else{
		cObjectManager::addObject( "obj_Sign", createBaseObject() );
		cObjectManager::at("obj_Sign")->setPosition( vec3(0, 5.5, 0) );
	}

	//pSignOldShape = new Shape;
	//if ( !pSignOldShape->init( "SignOldObject.lua", m_pEffect, true ))
	//		cwLogger::writeERROR("error init obj_OldSign");
	//else{
	//	cObjectManager::addObject( "obj_OldSign", createBaseObject() );
	//	cObjectManager::at("obj_OldSign")->setPosition( vec3(3, 4, 3));
	//	cObjectManager::at("obj_OldSign")->setIsUpdate(false);
	//	cObjectManager::at("obj_OldSign")->setRotate(0, -45, 0);
	//}

	pAssemblyShape = new Shape;
	if ( !pAssemblyShape->init( "AssemblyObject.lua", m_pEffect, true ) )
		cwLogger::writeERROR("Error init obj_Assembly");
	else{
		cObjectManager::addObject( "obj_Assembly", createBaseObject() );
		cObjectManager::at("obj_Assembly")->setPosition( vec3(0, 0, 0) );
	}

	pCoorGrid = new CoorGrid( 16, 16, 1.0 );
	pCoorGrid->init( pDebugEffect );

	std::string patch_toMusic = cFileManager::lookUpPath("audio/music/Ambient.ogg");
	cSoundManager::addSound( patch_toMusic,"snd_Ambient" );
	cSoundManager::at( "snd_Ambient" )->sound_Play();

	return true;
}

void GameWorld::ResetWorld()
{
	m_pRender = Kernel::spec().getRender();

	const int w = cParserCfg::getRenderCnfg()->width;
	const int h = cParserCfg::getRenderCnfg()->height;

	m_pRender->setViewport( 0, 0, w, h );
	pCamera->setPerspective( w / h );
}

void GameWorld::Draw()
{
	m_pRender->clear();

	pDebugEffect->update( pCamera );
	pCoorGrid->draw();
	pDebugEffect->endUseEffect();

	m_pEffect->update( pCamera );

	pSignShape->modelMtr() = cObjectManager::at("obj_Sign")->getTransform();
	pSignShape->draw();

	//pSignOldShape->modelMtr() = cObjectManager::at("obj_OldSign")->getTransform();
	//pSignOldShape->draw();

	pAssemblyShape->draw();

	m_pEffect->endUseEffect();

	m_pRender->swapBuffers();
}

void GameWorld::Update()
{
	float deltaTime = cTimer::getFrameTime() / cTimer::getMultiplier();

	if( GetKeyState(VK_ESCAPE) & 0x80 ) // TODO: only demo;
		Kernel::spec().QuitLoop();

	pCamera->update( deltaTime );

	cObjectManager::update( deltaTime );
	cSoundManager::update();
}